package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.ExamDao;
import com.chunfeng.mypen.dao.SubjectDao;
import com.chunfeng.mypen.dao.TopicDao;
import com.chunfeng.mypen.entity.Subject;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 学科方法实现类
 * @author: 小雷
 * @createTime: 2023-11-14 00:19:39
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, Subject> implements SubjectService {
    /**
     * 科目dao
     */
    @Autowired
    private SubjectDao subjectDao;
    /**
     * 试卷dao
     */
    @Autowired
    private ExamDao examDao;
    /**
     * 题目dao
     */
    @Autowired
    private TopicDao topicDao;

    /**
     * 分类查询所有学科
     *
     * @param subject 查询条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<Subject>> lookSubject(Subject subject) {
        List<Subject> subjectList = subjectDao.lookSubject(subject);
        if (subjectList.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(subjectList);
    }

    /**
     * 新增学科
     *
     * @param subject 新增内容
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> addSubject(Subject subject) {
        //判断该学科是否已经存在
        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<Subject>()
                .eq(!subject.getName().isEmpty(), Subject::getName, subject.getName()));
        if (count != 0) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        int insert = this.baseMapper.insert(subject);
        if (insert < 1) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(insert);
    }

    /**
     * 修改学科
     *
     * @param subject 修改内容
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateSubject(Subject subject) {
        //判断该学科是否已经存在
        Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<Subject>()
                .eq(!subject.getName().isEmpty(), Subject::getName, subject.getName()));
        if (count != 0) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        int update = this.baseMapper.updateById(subject);
        if (update < 1) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(update);
    }

    /**
     * 删除学科
     *
     * @param ids 删除的id
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteSubject(List<String> ids) {
        //判断接收的参数是否为空
        if (ids.isEmpty()) {
            JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //判断删除的id是否存在
        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<Subject>()
                .in(Subject::getId, ids));
        if (count == ids.size()) {
            JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //将该学科关联的试卷进行修改
        examDao.updateExamBySubjectId(ids);
        //将该学科关联的题目进行修改
        topicDao.updateTopicSubjectId(ids);
        //删除学科
        int delete = this.baseMapper.deleteBatchIds(ids);
        if (delete != count) {
            JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(delete);
    }
}
